<template>
	<view class="container">
		<tui-tabs :tabs="tabs" :isFixed="scrollTop>=0" :currentTab="currentTab" selectedColor="#ffa319" sliderBgColor="#ffa319"
		 @change="change" itemWidth="20%"></tui-tabs>
		<!--选项卡逻辑自己实现即可，此处未做处理-->
		<view :class="{'tui-order-list':scrollTop>=0}">
			<view class="tui-order-item" v-for="(model,orderIndex) in orderlist" :key="orderIndex">
				<tui-list-cell :hover="false" :lineLeft="false">
					<view class="tui-goods-title">
						<view>订单号：{{model.order_no}}</view>
						<view class="tui-order-status" v-if="model.order_status==0">待付款</view>
						<view class="tui-order-status" v-else-if="model.order_status==1">待发货</view>
						<view class="tui-order-status" v-else-if="model.order_status==2">待收货</view>
						<view class="tui-order-status" v-else-if="model.order_status==3">待评价</view>
						
					</view>
				</tui-list-cell>
				<block v-for="(item,index) in model.order_item_list" :key="index">
					<tui-list-cell padding="0" @click="detail(item.order_id)">
						<view class="tui-goods-item">
							<image :src="item.picture.pic_cover_small" class="tui-goods-img"></image>
							<view class="tui-goods-center">
								<view class="tui-goods-name">{{item.goods_name}}</view>
								<view class="tui-goods-attr">{{item.sku_name}}</view>
							</view>
							<view class="tui-price-right">
								<view>￥{{item.price}}</view>
								<view>x{{item.num}}</view>
								<view v-if="model.payment_type==3 && model.pay_status==2" class="yui-names">余额抵扣</view>
								<view v-else-if="model.pay_status==2 && model.payment_type==4" class="yui-names">余额抵扣</view>
							</view>
						</view>
					</tui-list-cell>
				</block>
				<tui-list-cell :hover="false" unlined>
					<view class="tui-goods-price">
						<view>共1件商品 合计：</view>
						<view class="tui-size-24">￥</view>
						<view class="tui-price-large">{{model.order_money}}</view>
						<view class="tui-size-24">.00</view>
					</view>
				</tui-list-cell>
				<view class="tui-order-btn">
				
					<view class="tui-btn-ml">
						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle" v-if="model.order_status==0" @click="detail(model.order_id)">去付款</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle" v-if="model.order_status==4" @click="detail(model.order_id)">申请售后</tui-button>
					    <tui-button type="black" plain width="152rpx" height="56rpx" :size="26" shape="circle" v-if="model.order_status==3" @click="detail(model.order_id)">去评价</tui-button>
						<tui-button type="black" plain width="152rpx" height="56rpx" :size="26" shape="circle" v-if="model.order_status==5">已失效</tui-button>
					</view>
					<view class="tui-btn-ml">
						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle" v-if="model.order_status==2" @click="detail(model.order_id)">确认收货</tui-button>
						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle" v-if="model.order_status==3" @click="detail(model.order_id)">再次购买</tui-button>
					</view>
				</view>
			</view>

			
		</view>
		<!--加载loadding-->
		<tui-loadmore v-if="loadding" :index="3" type="grey"></tui-loadmore>
		<tui-nomore v-if="!pullUpOn" backgroundColor="#fafafa"></tui-nomore>
		<view v-if="orderlist==''">
			<tui-no-data :fixed="false" imgUrl="/static/images/nodata/error.png">暂无数据</tui-no-data>
		</view>
		<!--加载loadding-->

	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [{
					name: "全部"
				}, {
					name: "待付款"
				}, {
					name: "待发货"
				}, {
					name: "待收货"
				}, {
					name: "待评价"
				}],
				currentTab: 0,
				pageIndex: 1,
				loadding: false,
				pullUpOn: true,
				scrollTop: 0,
				loading:'',
				loadingType:'',
				orderlist:'',
			}
		},
		onShow() {
			this.tui.haslogin();
			if(this.tui.isLogin()){
				this.init();
			}
		},
		onLoad: function(options) {
			if(options.currentTab>0){
				this.currentTab=options.currentTab;
			}
		},
		methods: {
			change(e) {
				this.currentTab = e.index;
				this.pageIndex=1;
				this.init();
			},
			init(type, loading){
				uni.showLoading({
					title: 'loading'
				});
				let token = this.tui.getToken();
				let status=this.currentTab==0?'all':this.currentTab-1;
				status=status==0?'0':status;
				this.pageIndex=1;
				let datas={
					token: token,
					page: this.pageIndex,
					order_status: status,
					limit: 10,
				};
				
				let result=this.tui.request('order','post',datas,1000,1,'','orderList');
				Promise.resolve(result).then((value)=>{
					this.loading = true;
					uni.hideLoading();
				   if(value.code==200){
				   	 this.orderlist=value.data.data;
				
				   }else{
				   	// this.tui.toast(value.message);
				   }
				})
			},
			detail(e) {
				uni.navigateTo({
					url: '/pages/orderDetail/orderDetail?order_id='+e
				})
			}
		},
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 200);
		},
		onReachBottom() {
			//只是测试效果，逻辑以实际数据为准
			this.loadding = true
			this.pullUpOn = true
			setTimeout(() => {
				this.loadding = false
				this.pullUpOn = false
			}, 1000)
			let token = this.tui.getToken();
			this.pageIndex=this.pageIndex+1;
			let status=this.currentTab==0?'all':this.currentTab;
			let datas={
				token: token,
				limit: 10,
				page: this.pageIndex,
				order_status: status,
			};
			
			let result=this.tui.request('order','post',datas,1000,1,'','orderList');
			Promise.resolve(result).then((value)=>{
				this.loading = true;
				uni.hideLoading();
			   if(value.code==200){
			   	 this.orderlist=this.orderlist.concat(value.data);
				
			   }else{
			   	this.tui.toast(value.message);
			   }
			})
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		}
	}
</script>

<style>
	.container {
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tui-order-list {
		margin-top: 80rpx;
	}

	.tui-order-item {
		margin-top: 20rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.tui-goods-title {
		width: 100%;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
    .yui-names{color:#e1b01a}
	.tui-order-status {
		color: #888;
		font-size: 26rpx;
	}

	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
	}

	.tui-goods-center {
		flex: 1;
		padding: 20rpx 8rpx;
		box-sizing: border-box;
	}

	.tui-goods-name {
		max-width: 310rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 26rpx;
		line-height: 32rpx;
	}

	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 20rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}

	.tui-color-red {
		color: #E41F19;
		padding-right: 30rpx;
	}

	.tui-goods-price {
		width: 100%;
		display: flex;
		align-items: flex-end;
		justify-content: flex-end;
		font-size: 24rpx;
	}

	.tui-size-24 {
		font-size: 24rpx;
		line-height: 24rpx;
	}

	.tui-price-large {
		font-size: 32rpx;
		line-height: 30rpx;
		font-weight: 500;
	}

	.tui-order-btn {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		background: #fff;
		padding: 10rpx 30rpx 20rpx;
		box-sizing: border-box;
	}

	.tui-btn-ml {
		margin-left: 20rpx;
	}
</style>
